<?php

require_once 'link.php';
require_once 'opendb.php';

class linkManager
{
    public static function addLink($link)
    {
        //$link = mysql_real_escape_string($link);
        $name = $link->getName();
        $userID = $link->getUserID();
        $address = $link->getAddress();
        $http = substr($address, 0, 7);
        if ($http == "http://")
        {
            $address = substr($address, 7);
        } else if ($http == "https:/")
        {
            $address = substr($address, 8);
        }
        $insert = mysql_query("INSERT INTO links VALUES('$userID', NULL, '$name', '$address')") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
        {
            echo "The link has been added successfully.";
        } else
            echo "The link has not been added successfully.";
        echo "<br>";
    }

    public static function editLink($link)
    {
        $link = mysql_real_escape_string($link);
        $name = $link->getName();
        $userID = $link->getUserID();
        $address = $link->getAddress();
        $linkID = $link->getLinkID();
        $update = mysql_query("UPDATE links SET UserID = '$userID', Name = '$name', Address = '$address' WHERE LinkID = '$linkID' LIMIT 1") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The link has been updated successfully.";
        else
            echo "The link has not been updated successfully.";
        echo "<br>";
    }

    public static function deleteLink($link)
    {
        $link = mysql_real_escape_string($link);
        $linkID = $link->getLinkID();
        $delete = mysql_query("DELETE FROM links WHERE LinkID = '$linkID' LIMIT 1");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The link has been successfully deleted.";
        else
            echo "The link has not been successfully deleted.";
        echo "<br>";
    }

    public static function getLinks($userID)
    {
       // $userID = mysql_real_escape_string(userID);
        $select = mysql_query("SELECT * FROM links WHERE UserID = '$userID'") or die(mysql_error());
        $linkArray = array();
        while ($row = mysql_fetch_array($select))
        {
            $link = new Link();
            $link = linkManager::getLink($row['LinkID']);
            $linkArray[] = $link;
        }
        return $linkArray;
    }

    public static function getLink($linkID)
    {
      //  $linkID = mysql_real_escape_string($linkID);
        $select = mysql_query("SELECT * FROM links WHERE LinkID = '$linkID'") or die(mysql_error());
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $linkInfo = mysql_fetch_array($select);
            $link = new Link();
            $link->setUserID($linkInfo['UserID']);
            $link->setLinkID($linkInfo['LinkID']);
            $link->setName($linkInfo['Name']);
            $link->setAddress($linkInfo['Address']);
            return $link;
        } else
            echo "There is no link with that ID.";
    }
}
?>